"""
    Cash register controller.
"""

from turbogears import widgets, expose, validate, redirect
from mnemoply import model
from mnemoply.widgets import DictField, DictionaryField
from mnemoply.controllers.lookup import LookupController, TableCol, TableLookupDataSource
from mnemoply.controllers.crud import CRUDController, ItemTab

class DepartmentItemTab(ItemTab):
    """Tab with CashRegisterDepartments."""
    title = _("Cash register department")
    name = "cash_register_department_tab"
    template = "../cash_register_department_tab"

    add_form = widgets.TableForm(
        fields=[
            DictionaryField(
                name="name_id",
                model_object_name='CashRegisterDepartmentName',
                not_empty=True,
                label=_("Cash register department name"),
            ),
            DictField(name='ordinal', not_empty=True,
                      label=_("Ordinal"), data=dict([(x, x) for x in range(1, 17)])),
        ],
        submit_text=_("Add"),
        action='add_department',
    )

    @validate(form=add_form)
    @expose()
    def add_department(self, obj, name_id, ordinal):
        name = model.CashRegisterDepartmentName.get(int(name_id))
        model.CashRegisterDepartment(cashRegister=obj, name=name, ordinal=ordinal)
        raise redirect("./")

    @expose()
    def delete_department(self, obj, department_id):
        model.CashRegisterDepartment.get(int(department_id)).destroySelf()
        raise redirect("./")

class DepartmentTrendItemTab(ItemTab):
    """Tab with CashRegisterDepartmentTrends."""
    title = _("Cash register department trend")
    name = "cash_register_department_trend_tab"
    template = "../cash_register_department_trend_tab"

    add_form = widgets.TableForm(
        fields=[
            DictionaryField(
                name="department_id",
                model_object_name='CashRegisterDepartment',
                not_empty=True,
                label=_("Cash register department"),
            ),
            DictionaryField(name='trend_id', model_object_name='Trend', not_empty=True, label=_("Trend")),
        ],
        submit_text=_("Add"),
        action='add_department_trend',
    )

    @validate(form=add_form)
    @expose()
    def add_department_trend(self, obj, department_id, trend_id):
        department = model.CashRegisterDepartment.get(int(department_id))
        trend = model.Trend.get(int(trend_id))
        model.CashRegisterDepartmentTrend(department=department, trend=trend)
        raise redirect("./")

    @expose()
    def delete_department_trend(self, obj, dt_id):
        model.CashRegisterDepartmentTrend.get(int(dt_id)).destroySelf()
        raise redirect("./")

class BondRecordTrendItemTab(ItemTab):
    """Tab with bond records fot this cash register."""
    title = _("Bond record")
    name = "cash_register_bond_record_tab"
    template = "../cash_register_bond_record_tab"

class CashRegisterController(LookupController, CRUDController):
    model_object = model.CashRegister
    lookup_columns = [
        TableCol(name='name', caption=_('Name')),
        TableCol(name='office', caption=_('Office')),
        TableCol(name='organization', caption=_('Organization')),
    ]
    view_fields = ['name', 'officeID', 'organizationID']
    item_tabs = [DepartmentItemTab(), DepartmentTrendItemTab(), BondRecordTrendItemTab()]

    def __init__(self):
        LookupController.__init__(self,
            data_source=TableLookupDataSource('cash_register_lookup'),
            columns=self.lookup_columns,
            title=self.model_object.model_title,
        )
        CRUDController.__init__(self, model_object=self.model_object)

exposed = CashRegisterController()
